﻿@page "/lesson/update/{Id}"

@inject ILessonService LessonService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@inject IMainPlanService MainPlanService

<PageTitle>更新经验教训描述</PageTitle>
<div class="row">
    <div class="col">
        <h3>@_mainPlanDto.Number-@_mainPlanDto.Name</h3>
        <a href="lessons/mainplan/@_mainPlanDto.Id">[查看项目经验教训]</a>
    </div>
    <CompDeleteIcon Roles="admin,mgr,pm" Click="Delete" />
</div>
<EditForm Model="_lessonDto" OnSubmit="Save">
    <div class="input-group mb-2">
        <span class="input-group-text">经验内容</span>
        <textarea class="form-control" placeholder="请详细描述经验教训，可输入多行" style="height: 180px" @bind="@_lessonDto.Content"></textarea>
    </div>
    <ImagesUpload Title="经验图片(可添加多个)" @bind-MultiImageUrl="@_lessonDto.ContentUrl" />

    <CompSaveCancelButtons CancelUrl="@($"lessons/mainplan/{_lessonDto.MainPlanId}")" />
</EditForm>
@code {
    [Parameter]
    public string Id { get; set; }
    private LessonDto _lessonDto = new();
    private MainPlanDto _mainPlanDto = new();

    protected override async Task OnInitializedAsync()
    {
        var lResult = await LessonService.GetFirstOrDefaultAsync(Guid.Parse(Id));
        _lessonDto = lResult.Result;

        var mResult = await MainPlanService.GetFirstOrDefaultAsync(_lessonDto.MainPlanId);
        _mainPlanDto = mResult.Result;
    }

    private async Task Delete()
    {
        var confirmResult = await JsRuntime.InvokeAsync<bool>("confirm", "确定要删除当前经验教训吗？");
        if (confirmResult)
        {
            var result = await LessonService.DeleteAsync(Guid.Parse(Id));
            if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"lessons/mainplan/{_lessonDto.MainPlanId}");
        }
    }
    private async Task Save()
    {
        var result = await LessonService.UpdateAsync(Guid.Parse(Id), _lessonDto);
         if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"lessons/mainplan/{_lessonDto.MainPlanId}");
    }
}
